﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Web.Framework;
using Job.Logic.Personal;
using Job.Entity;
using Job.Logic.Common;

namespace Job.Web.userControls.Personal
{
    public partial class Skills :BaseCtrl
    {
        int _resumeId = myR["resumeid"].IntValue;
        int _skillId = myR["skillid"].IntValue;
        int subjectid = myR["subjectid"].IntValue;

        PersonalLogic pl = new PersonalLogic();
        private void ShowMessage(string message)
        {
            this.message.InnerHtml = "<font color=red>" + message + "</font>";
            this.message.Visible = true;
            this.message.Height = "30px";
            this.message.VAlign = "middle";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.LoadData();
            }
        }

        private void LoadData()
        {
            try
            {
                JobPersonSkill skill = pl.GetSkill(_skillId);
                string skillName1 = "";
                string skillName2 = "";
                string skillLevel = "";
                if (skill != null)
                {
                    skillName1 = Constants.GetParentCodeBySubCode(skill.SkillName.Trim());
                    skillName2 = skill.SkillName.Trim();
                    txtUseLen.Text = skill.UseLen.HasValue == false ? "" : skill.UseLen.Value.ToString();
                    skillLevel = skill.SkillLevel.HasValue == false ? "" : skill.SkillLevel.Value.ToString();

                }
                WebCommon.LoadLevelDropDownList(ddlName1, ddlName2, RelatedListName, skillName1, skillName2, Constants.DictionaryType.IT_ABILITY);
                WebCommon.SetDropDownFromDict(ddlLevel, skillLevel, Constants.GetDictionaryCode(Constants.DictionaryType.LANG_SKILL), true);

                gvSkill.DataSource = pl.GetSkillList(_resumeId);
                gvSkill.DataBind();
            }
            catch (Exception ex)
            {
                ShowMessage("IT技能查询失败:<br>" + ex.Message);
                Logger.WriteLog("IT技能查询失败:\r\n" + ex.Message);
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                JobPersonSkill skill = new JobPersonSkill();
                skill.PersonInfoId = _resumeId;
                skill.PersonSkillId = _skillId;
                int level = 0;
                Int32.TryParse(ddlLevel.SelectedValue.ToString(), out level);
                if (level > 0)
                {
                    skill.SkillLevel = level;
                }
                skill.SkillName = ddlName2.SelectedValue.Trim();
                skill.UseLen = Convert.ToInt32(txtUseLen.Text.Trim());
                if (_skillId > 0)
                {
                    pl.UpdateSkill(skill);
                }
                else
                {
                    pl.InsertSkill(skill);
                }
            }
            catch (Exception ex)
            {
                ShowMessage("保存失败:<br>" + ex.Message);
                Logger.WriteLog("保存失败:\r\n" + ex.Message);
                return;
            }
            string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this._resumeId;
            Response.Redirect(url);
        }

        protected void gvSkill_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void gvSkill_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName.ToLower() == "edit")
            {
                //edit
                string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this._resumeId + "&skillid=" + id;
                Response.Redirect(url);

            }
            else if (e.CommandName.ToLower() == "delete")
            {
                JobPersonSkill skill = pl.GetSkill(id);
                pl.DeleteSkill(skill);

                string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this._resumeId;
                Response.Redirect(url);
            }
 
        }

        protected void gvSkill_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblLevelName = (Label)e.Row.FindControl("lblLevelName");
                Label lblLevel = (Label)e.Row.FindControl("lblLevel");
                Label lblSKillName = (Label)e.Row.FindControl("lblSKillName");
                Label lblName = (Label)e.Row.FindControl("lblName");
                
                int level = 0;
                Int32.TryParse(lblLevel.Text.Trim(), out level);
                lblLevelName.Text = MemoryDictionary.GetCommonDictName(level);
                lblSKillName.Text = MemoryDictionary.GetFullItAbilityName(lblName.Text.Trim());
            }
        }
    }
}